package com.ssyx.cart.controller;

import com.ssyx.cart.feign.ActivityFeignClient;
import com.ssyx.cart.service.CartInfoService;
import com.ssyx.common.auth.AuthContextHolder;
import com.ssyx.common.result.Result;
import com.ssyx.model.activity.ActivityRule;
import com.ssyx.model.order.CartInfo;
import com.ssyx.vo.order.OrderConfirmVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:CartApiController
 * Description:
 *
 * @Author kay
 * @Create 2024/5/19 16:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    private CartInfoService cartInfoService;

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //添加商品到购物车
    //添加内容有：当前用户id,skuId,商品数量
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum){
        //获取当前用户id
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.addToCart(userId,skuId,skuNum);
        return Result.ok(null);
    }

    //根据skuId,删除购物车中对应的商品sku
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteSkuInCart(@PathVariable("skuId") Long skuId,
                             HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.deleteSkuInCart(skuId, userId);
        return Result.ok(null);
    }

    //批量删除购物车商品
    @PostMapping("batchDeleteCart")
    public Result batchDeleteCart(@RequestBody List<Long> skuIdList, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.batchDeleteCart(skuIdList, userId);
        return Result.ok(null);
    }

    //清空购物车
    @DeleteMapping("deleteAllCart")
    public Result deleteAllCart(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.deleteAllCart(userId);
        return Result.ok(null);
    }

    //购物车列表
    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);
        return Result.ok(cartInfoList);
    }

    //带优惠活动及优惠券的购物车,此类购物车相关数据封装在类OrderConfirmVo中
    @GetMapping("activityCartList")
    public Result activityCartList() {
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        List<CartInfo> cartInfoList = cartInfoService.getCartList(userId);

        //普通购物车列表 -> 商品项列表sku的信息 -> 优惠活动信息
        //userId -> 当前用户的优惠券(表coupon_use) -> 优惠券的优惠信息
        OrderConfirmVo orderTradeVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList,userId);
        return Result.ok(orderTradeVo);
    }

    //更新购物车单个商品项的选中状态
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable(value = "skuId") Long skuId,
                            @PathVariable(value = "isChecked") Integer isChecked, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.checkCart(userId, isChecked, skuId);
        return Result.ok(null);
    }

    //更新购物车全选状态
    @GetMapping("checkAllCart/{isChecked}")
    public Result checkAllCart(@PathVariable(value = "isChecked") Integer isChecked,
                               HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.checkAllCart(userId, isChecked);
        return Result.ok(null);
    }

    //修改购物车中批量商品项的选中状态
    @PostMapping("batchCheckCart/{isChecked}")
    public Result batchCheckCart(@RequestBody List<Long> skuIdList,
                                 @PathVariable(value = "isChecked") Integer isChecked,
                                 HttpServletRequest request){
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        cartInfoService.batchCheckCart(skuIdList, userId, isChecked);
        return Result.ok(null);
    }
}
